package com.qsxc.business.bean;

import com.qsxc.utils.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.Map;

/**
 * 统一API响应结果封装
 */
@ApiModel(description = "返回信息")
@Data
@ToString
@Builder
//@NoArgsConstructor
@AllArgsConstructor
public class R<T> implements Serializable {
    public static final int SUCCESS = 1;
    public static final int FAIL = 1;
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "状态码 1成功 0通用失败", required = true)
    private int code;
    @ApiModelProperty(value = "承载数据")
    private T data;
    @ApiModelProperty(value = "返回消息", required = true)
    private String msg;

    private R(ResultCode resultCode) {
        this(resultCode.code, null, resultCode.message);
    }

    private R(ResultCode resultCode, String msg) {
        this(resultCode.code, null, msg);
    }

    private R(ResultCode resultCode, T data) {
        this(resultCode.code, data, resultCode.getMessage());
    }

    private R(ResultCode resultCode, T data, String msg) {
        this(resultCode.code, data, msg);
    }

    /**
     * 返回R
     *
     * @param <T> T 泛型标记
     * @return R
     */
    public static <T> R<T> success() {
        return new R<>(ResultCode.CUSTOM_SUCCESS);
    }

    /**
     * 返回R
     *
     * @param data 数据
     * @param <T>  T 泛型标记
     * @return R
     */
    public static <T> R<T> success(T data) {
        return new R<>(ResultCode.CUSTOM_SUCCESS, data);
    }

    /**
     * 返回R
     *
     * @param data 数据
     * @param msg  消息
     * @param <T>  T 泛型标记
     * @return R
     */
    public static <T> R<T> success(T data, String msg) {
        return new R<>(ResultCode.CUSTOM_SUCCESS, data, msg);
    }

    /**
     * 返回R
     *
     * @param code 状态码
     * @param data 数据
     * @param msg  消息
     * @param <T>  T 泛型标记
     * @return R
     */
    public static <T> R<T> success(int code, T data, String msg) {
        return new R<>(code, data, msg);
    }

    /**
     * 返回R
     *
     * @param <T> T 泛型标记
     * @param map
     * @return R
     */
    public static <T> R<T> fail(Map<String, Object> map) {
        return new R<>(ResultCode.CUSTOM_FAIL);
    }

    /**
     * 返回R
     *
     * @param msg 消息
     * @param <T> T 泛型标记
     * @return R
     */
    public static <T> R<T> fail(String msg) {
        return new R<>(ResultCode.CUSTOM_FAIL, StringUtil.isEmpty(msg) ? "执行失败" : msg);
    }

    /**
     * 返回R
     *
     * @param code 状态码
     * @param msg  消息
     * @param <T>  T 泛型标记
     * @return R
     */
    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, null, StringUtil.isEmpty(msg) ? "执行失败" : msg);
    }

    /**
     * 返回R
     *
     * @param resultCode 业务代码
     * @param <T>        T 泛型标记
     * @return R
     */
    public static <T> R<T> fail(ResultCode resultCode) {
        return new R<>(resultCode.code, null, resultCode.message);
    }
}
